

let redBagBaseUrl = "https://www.l3y.cn";



// 红包代码

// html：

let redBagHtml = `<div id="l3y-red-bag-layer"><img src="${redBagBaseUrl}/images/hongbao/hongbao88.png"></div>`;



let redBagImgList = ["hongbao88.png", "hongbao188.png", "hongbao588.png", "hongbao888.png"];



let redBagImgIndex = 1;



function changeRedBagImg() {

    if (redBagImgIndex >= redBagImgList.length) {

        redBagImgIndex = 0

    }

    $("#l3y-red-bag-layer img").attr("src", `${redBagBaseUrl}/images/hongbao/${redBagImgList[redBagImgIndex]}`)

    redBagImgIndex++;

}



/*

*   speed //元素移动速度

*   xPos  //元素一开始左距离

*   yPos  //元素一开始上距离

*   $('#div1').imgFloat({speed:30,xPos:10,yPos:10});

*   $('#div2').imgFloat();   //不给参数默认（speed:15,xPos:0,yPos:0）

*/

// 页面写入html

(function($) {

    $("body").append(redBagHtml)

    // css

    $("#l3y-red-bag-layer").css({

        "position": "fixed",

        "width": "73px",

        "height":"87px",

        "z-index": "99999",

    });

    $("#l3y-red-bag-layer img").css({

        "width": "100%",

        "height":"100%"

    })

    // 改图

    setInterval(changeRedBagImg, 1000);

    // 飘动

    jQuery.fn.imgFloat = function(options) {

        var own = this;

        var xD = 0;

        var yD = 0;

        var i = 1;

        var settings = {

            speed: 15,

            xPos: 0,

            yPos: 0

        };

        jQuery.extend(settings, options);

        var ownTop = settings.xPos;

        var ownLeft = settings.yPos;

        own.css({

            position: "fixed",

            cursor: "pointer"

        });



        function imgPosition() {

            var winWidth = $(window).width() - own.width();

            var winHeight = $(window).height() - own.height();

            if (xD == 0) {

                ownLeft += i;

                own.css({

                    left: ownLeft

                });

                if (ownLeft >= winWidth) {

                    ownLeft = winWidth;

                    xD = 1;

                }

            }

            if (xD == 1) {

                ownLeft -= i;

                own.css({

                    left: ownLeft

                });

                if (ownLeft <= 0) xD = 0;

            }

            if (yD == 0) {

                ownTop += i;

                own.css({

                    top: ownTop

                });

                if (ownTop >= winHeight) {

                    ownTop = winHeight;

                    yD = 1;

                }

            }

            if (yD == 1) {

                ownTop -= i;

                own.css({

                    top: ownTop

                });

                if (ownTop <= 0) yD = 0;

            }

        }

        var imgHover = setInterval(imgPosition, settings.speed);

        own.hover(function() {

                clearInterval(imgHover);

            },

            function() {

                imgHover = setInterval(imgPosition, settings.speed);

            });

    }



    // 点击事件

    $("#l3y-red-bag-layer").on("click", function (){

        console.log("click");

        window.open(redBagBaseUrl, '_blank');

    })

})(jQuery);



//$("#img").imgFloat();   //不给参数默认（speed:10,xPos:0,yPos:0）

$("#l3y-red-bag-layer").imgFloat({

    speed: 15,

    xPos: 10,

    yPos: 10

});





